package com.kstech.ks_gaz01.realTime

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankii.androidlibrary.utils.CalcUtil
import com.chad.library.adapter4.BaseMultiItemAdapter

import com.kstech.ks_gaz01.XML_SPECIAL
import com.kstech.ks_gaz01.data.DebugDataBean
import com.kstech.ks_gaz01.databinding.LayoutItemOutriggerBinding
import com.kstech.ks_gaz01.databinding.LayoutRealtimeFloatLowBinding

class FloatMultiAdapter(data: List<DebugDataBean.Item>) :
    BaseMultiItemAdapter<DebugDataBean.Item>(data) {
    // 类型 1 的 viewholder
    class ItemVH(val viewBinding: LayoutRealtimeFloatLowBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    // 类型 2 的 viewholder
    class HeaderVH(val viewBinding: LayoutItemOutriggerBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    init {
        addItemType(ITEM_TYPE, object : OnMultiItemAdapterListener<DebugDataBean.Item, ItemVH> {
            override fun onBind(holder: ItemVH, position: Int, item: DebugDataBean.Item?) {
                // 绑定 item 数据
//                holder.viewBinding.address.text = item!!.para0 //地址
                holder.viewBinding.itemName.text = item!!.para1//项目名
                holder.viewBinding.unit.text = item!!.para5//单位
                holder.viewBinding.value.text = item.revData//从控制器接收的参数
            }

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVH {
                // 创建 viewholder
                val viewBinding =
                    LayoutRealtimeFloatLowBinding.inflate(
                        LayoutInflater.from(context),
                        parent,
                        false
                    )
                return ItemVH(viewBinding)
            }

        }).addItemType(
            SPECIAL_TYPE,
            object : OnMultiItemAdapterListener<DebugDataBean.Item, HeaderVH> {
                override fun onBind(holder: HeaderVH, position: Int, item: DebugDataBean.Item?) {

                    holder.viewBinding.itemName.text = item!!.para1
                    // 绑定 item 数据
                    if (item!!.revData.isNullOrEmpty() || item!!.revData == "00") {
                        return
                    }
//                    item!!.revData 十进制
//                    globalPrintln("支腿状态：${item!!.revData}")
                    val binaryString = CalcUtil.Int2BinaryString(item!!.revData.toInt())
                    val zt1_value = binaryString.substring(0, 3)
                    val zt2_value = binaryString.substring(3, 6)
                    val zt3_value = binaryString.substring(6, 9)
                    val zt4_value = binaryString.substring(9, 12)

                    holder.viewBinding.zt1.text = getState(Integer.valueOf(zt1_value, 2))
                    holder.viewBinding.zt2.text = getState(Integer.valueOf(zt2_value, 2))
                    holder.viewBinding.zt3.text = getState(Integer.valueOf(zt3_value, 2))
                    holder.viewBinding.zt4.text = getState(Integer.valueOf(zt4_value, 2))

                    val zt1_state = binaryString.substring(12, 13)
                    val zt2_state = binaryString.substring(13, 14)
                    val zt3_state = binaryString.substring(14, 15)
                    val zt4_state = binaryString.substring(15, 16)

                    holder.viewBinding.zt1.isSelected = zt1_state == "1"
                    holder.viewBinding.zt2.isSelected = zt2_state == "1"
                    holder.viewBinding.zt3.isSelected = zt3_state == "1"
                    holder.viewBinding.zt4.isSelected = zt4_state == "1"


                }

                override fun onCreate(
                    context: Context,
                    parent: ViewGroup,
                    viewType: Int
                ): HeaderVH {
                    // 创建 viewholder
                    val viewBinding = LayoutItemOutriggerBinding.inflate(
                        LayoutInflater.from(context),
                        parent,
                        false
                    )
                    return HeaderVH(viewBinding)
                }

                override fun isFullSpanItem(itemType: Int): Boolean {
                    // 使用GridLayoutManager时，此类型的 item 是否是满跨度
                    return false
                }

            }).onItemViewType { position, list -> // 根据数据，返回对应的 ItemViewType
            if (list[position].para2 == XML_SPECIAL) {
                SPECIAL_TYPE
            } else {
                ITEM_TYPE
            }
        }
    }

    companion object {
        private const val ITEM_TYPE = 0
        private const val SPECIAL_TYPE = 1
    }

    private fun getState(i: Int): String {
        when (i) {
            1 -> return "Min"
            2 -> return "Mid1"
            3 -> return "Mid2"
            4 -> return "Max"
        }
        return "Min"
    }
}